package com.xpalive.config.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.xpalive.config.mybatis.method.AlwaysUpdateById;
import com.xpalive.config.mybatis.method.DelPermanent;
import com.xpalive.config.mybatis.method.DelPermanentBatchByIds;
import com.xpalive.config.mybatis.method.DelPermanentById;
import com.xpalive.config.mybatis.method.DelPermanentByMap;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        //autoCountSql 优化sql
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(
            DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(false);
        paginationInnerInterceptor.setMaxLimit(500L);

        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    /**
     * 自定义 SqlInjector 里面包含自定义的全局方法
     */
    @Bean
    public DefaultSqlInjector galaxySqlInjector() {
        return new DefaultSqlInjector() {
            @Override
            public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
                List<AbstractMethod> methodList = super.getMethodList(mapperClass);
                methodList.add(new DelPermanent());
                methodList.add(new DelPermanentBatchByIds());
                methodList.add(new DelPermanentById());
                methodList.add(new DelPermanentByMap());
                methodList.add(new AlwaysUpdateById());
                return methodList;
            }
        };
    }
}
